﻿Public Class JSONArray
    Dim values As ArrayList
    Public Sub New()
        values = New ArrayList()
    End Sub
    ''' <summary>
    ''' 通过Object对象构造实体
    ''' </summary>
    ''' <param name="obj"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal obj As Object)
        If IsArray(obj) Or obj.GetType.Equals(GetType(IList(Of Object))) Or obj.GetType.Equals(GetType(List(Of Object))) Then
            values = ArrayList.Adapter(obj)
        ElseIf obj.GetType.Equals(GetType(HashSet(Of Object))) Or obj.GetType.Equals(GetType(SortedSet(Of Object))) Then
            values = ArrayList.Adapter(obj.ToArray())
        ElseIf obj.GetType.Equals(GetType(ArrayList)) Then
            values = obj
        Else
            values = New ArrayList()
            values.Add(obj)
        End If
    End Sub
    ''' <summary>
    ''' 通过数组列表构造实体
    ''' </summary>
    ''' <param name="arrBuff"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal arrBuff As ArrayList)
        values = arrBuff
    End Sub
    ''' <summary>
    ''' 通过数组构造实体
    ''' </summary>
    ''' <param name="arrBuff"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal arrBuff As Array)
        values = ArrayList.Adapter(arrBuff)
    End Sub
    ''' <summary>
    ''' 通过列表构造实体
    ''' </summary>
    ''' <param name="list"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal list As List(Of Object))
        values = ArrayList.Adapter(list)
    End Sub
    ''' <summary>
    ''' 通过列表构造实体
    ''' </summary>
    ''' <param name="hSet"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal hSet As HashSet(Of Object))
        values = ArrayList.Adapter(hSet.ToArray())
    End Sub
    ''' <summary>
    ''' 通过列表构造实体
    ''' </summary>
    ''' <param name="sSet"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal sSet As SortedSet(Of Object))
        values = ArrayList.Adapter(sSet.ToArray())
    End Sub
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal value As Boolean) As JSONArray
        values.Add(value)
        ' Me.Add(value)
        Return Me
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal value As Byte) As JSONArray
        values.Add(value)
        'Me.Add(value)
        Return Me
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal value As SByte) As JSONArray
        values.Add(value)
        'Me.Add(value)
        Return Me
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal value As Char) As JSONArray
        values.Add(value)
        'Me.Add(value)
        Return Me
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal value As Double) As JSONArray
        values.Add(value)
        'Me.Add(value)
        Return Me
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal value As Single) As JSONArray
        values.Add(value)
        'Me.Add(value)
        Return Me
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal value As Short) As JSONArray
        values.Add(value)
        'Me.Add(value)
        Return Me
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal value As UShort) As JSONArray
        values.Add(value)
        ' Me.Add(value)
        Return Me
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal value As Integer) As JSONArray
        values.Add(value)
        'Me.Add(value)
        Return Me
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal value As UInteger) As JSONArray
        values.Add(value)
        ' Me.Add(value)
        Return Me
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal value As Long) As JSONArray
        values.Add(value)
        'Me.Add(value)
        Return Me
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal value As ULong) As JSONArray
        values.Add(value)
        ' Me.Add(value)
        Return Me
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal value As String) As JSONArray
        values.Add(value)
        'Me.Add(value)
        Return Me
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal value As Object) As JSONArray
        values.Add(value)
        ' Me.Add(value)
        Return Me
    End Function
    ''' <summary>
    ''' 与<c>put</c>相同，增加了有效性检查。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <remarks></remarks>
    Public Sub CheckedPut(ByVal value As Object)
        If IsNumeric(value) Then
            JSON.checkDouble(value)
        End If
        Put(value)
    End Sub
    Public Function Put(ByVal index As Integer, ByVal value As Object) As JSONArray
        If IsNumeric(value) Then
            JSON.checkDouble(value)
        End If
        While values.Count <= index
            values.Add(Nothing)
        End While
        values.Insert(index, value)
        Return Me
    End Function
    Public Function Put(ByVal index As Integer, ByVal value As Boolean) As JSONArray
        Dim obj As Object = value
        Return Put(index, obj)
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal index As Integer, ByVal value As Byte) As JSONArray
        Dim obj As Object = value
        Return Put(index, obj)
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal index As Integer, ByVal value As SByte) As JSONArray
        Dim obj As Object = value
        Return Put(index, obj)
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal index As Integer, ByVal value As Char) As JSONArray
        Dim obj As Object = value
        Return Put(index, obj)
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal index As Integer, ByVal value As Double) As JSONArray
        Dim obj As Object = value
        Return Put(index, obj)
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal index As Integer, ByVal value As Single) As JSONArray
        Dim obj As Object = value
        Return Put(index, obj)
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal index As Integer, ByVal value As Short) As JSONArray
        Dim obj As Object = value
        Return Put(index, obj)
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal index As Integer, ByVal value As UShort) As JSONArray
        Dim obj As Object = value
        Return Put(index, obj)
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal index As Integer, ByVal value As Integer) As JSONArray
        Dim obj As Object = value
        Return Put(index, obj)
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal index As Integer, ByVal value As UInteger) As JSONArray
        Dim obj As Object = value
        Return Put(index, obj)
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal index As Integer, ByVal value As Long) As JSONArray
        Dim obj As Object = value
        Return Put(index, obj)
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal index As Integer, ByVal value As ULong) As JSONArray
        Dim obj As Object = value
        Return Put(index, obj)
    End Function
    ''' <summary>
    ''' 将<code>value</code>追加到此数组的结尾。
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Put(ByVal index As Integer, ByVal value As String) As JSONArray
        Dim obj As Object = value
        Return Put(index, obj)
    End Function
    ''' <summary>
    ''' 返回一个 Boolean 值，指示数组指定位置是否未被指派对象。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function IsNothing(ByVal index As Integer) As Boolean
        Dim val As Object = Opt(index)
        Return val = Nothing
    End Function
    Public Function GetItem(ByVal index As Integer) As Object
        Try
            Dim val As Object = values.Item(index)
            If Microsoft.VisualBasic.IsNothing(val) Then
                Throw New Exception("Value at " + index + " is null.")
            End If
            Return val
        Catch ex As Exception
            Throw New Exception("Index " + index + " out of range [0.." + values.Count() + ")", ex)
        End Try
    End Function

    Public Function Opt(ByVal index As Integer) As Object
        If index < 0 Or index >= values.Count Then
            Return Nothing
        End If
        Return values.Item(index)
    End Function
    ''' <summary>
    ''' 删除并返回<code>index</code>处的值，如果数组在<code>index</code>处没有值，则返回nothing。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Remove(ByVal index As Integer) As Object
        Dim val As Object = Opt(index)
        If Microsoft.VisualBasic.IsNothing(val) Then
            Return Nothing
        Else
            'Me.Remove(val)
            values.Remove(val)
            Return val
        End If
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果它存在且是布尔值或可以强制为布尔值）。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' <exception cref="Exception">如果<code>index</code>处的值不存在或无法强制为布尔值。</exception>
    Public Function GetBoolean(ByVal index As Integer) As Boolean
        Dim val As Object = GetItem(index)
        Dim result As Boolean = JSON.toBoolean(val)
        If Microsoft.VisualBasic.IsNothing(result) Then
            Throw JSON.typeMismatch(index, val, GetType(Boolean).Name)
        End If
        Return result
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果该值存在并且是布尔值或可以*强制为布尔值）。否则返回<code>defaultVal</code>。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <param name="defaultVal"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OptBoolean(ByVal index As Integer, Optional ByVal defaultVal As Boolean = False) As Boolean
        Dim val As Object = Opt(index)
        Dim result As Boolean = JSON.toBoolean(val)
        Return IIf(Microsoft.VisualBasic.IsNothing(result), defaultVal, result)
    End Function

    ''' <summary>
    ''' 返回<code>index</code>处的值（如果它存在且是Byte或可以强制为Byte）。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' <exception cref="Exception">如果<code>index</code>处的值不存在或无法强制为Byte。</exception>
    Public Function GetByte(ByVal index As Integer) As Byte
        Dim val As Object = GetItem(index)
        Dim result As Byte = JSON.toByte(val)
        If Microsoft.VisualBasic.IsNothing(result) Then
            Throw JSON.typeMismatch(index, val, GetType(Byte).Name)
        End If
        Return result
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果该值存在并且是Byte或可以*强制为Byte）。否则返回<code>defaultVal</code>。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <param name="defaultVal"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OptByte(ByVal index As Integer, Optional ByVal defaultVal As Byte = 0) As Byte
        Dim val As Object = Opt(index)
        Dim result As Byte = JSON.toByte(val)
        Return IIf(Microsoft.VisualBasic.IsNothing(result), defaultVal, result)
    End Function

    ''' <summary>
    ''' 返回<code>index</code>处的值（如果它存在且是Char或可以强制为Char）。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' <exception cref="Exception">如果<code>index</code>处的值不存在或无法强制为Char。</exception>
    Public Function GetChar(ByVal index As Integer) As Char
        Dim val As Object = GetItem(index)
        Dim result As Char = JSON.toChar(val)
        If Microsoft.VisualBasic.IsNothing(result) Then
            Throw JSON.typeMismatch(index, val, GetType(Char).Name)
        End If
        Return result
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果该值存在并且是Char或可以*强制为Char）。否则返回<code>defaultVal</code>。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <param name="defaultVal"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OptByte(ByVal index As Integer, Optional ByVal defaultVal As Char = Chr(0)) As Char
        Dim val As Object = Opt(index)
        Dim result As Char = JSON.toChar(val)
        Return IIf(Microsoft.VisualBasic.IsNothing(result), defaultVal, result)
    End Function

    ''' <summary>
    ''' 返回<code>index</code>处的值（如果它存在且是SByte或可以强制为SByte）。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' <exception cref="Exception">如果<code>index</code>处的值不存在或无法强制为SByte。</exception>
    Public Function GetSByte(ByVal index As Integer) As SByte
        Dim val As Object = GetItem(index)
        Dim result As SByte = JSON.toSByte(val)
        If Microsoft.VisualBasic.IsNothing(result) Then
            Throw JSON.typeMismatch(index, val, GetType(SByte).Name)
        End If
        Return result
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果该值存在并且是SByte或可以*强制为SByte）。否则返回<code>defaultVal</code>。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <param name="defaultVal"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OptSByte(ByVal index As Integer, Optional ByVal defaultVal As SByte = 0) As SByte
        Dim val As Object = Opt(index)
        Dim result As SByte = JSON.toSByte(val)
        Return IIf(Microsoft.VisualBasic.IsNothing(result), defaultVal, result)
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果它存在且是Short或可以强制为Short）。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' <exception cref="Exception">如果<code>index</code>处的值不存在或无法强制为Short。</exception>
    Public Function GetShort(ByVal index As Integer) As Short
        Dim val As Object = GetItem(index)
        Dim result As Short = JSON.toShort(val)
        If Microsoft.VisualBasic.IsNothing(result) Then
            Throw JSON.typeMismatch(index, val, GetType(Short).Name)
        End If
        Return result
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果该值存在并且是Short或可以*强制为Short）。否则返回<code>defaultVal</code>。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <param name="defaultVal"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OptShort(ByVal index As Integer, Optional ByVal defaultVal As Short = 0) As Short
        Dim val As Object = Opt(index)
        Dim result As Short = JSON.toShort(val)
        Return IIf(Microsoft.VisualBasic.IsNothing(result), defaultVal, result)
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果它存在且是UShort或可以强制为UShort）。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' <exception cref="Exception">如果<code>index</code>处的值不存在或无法强制为UShort。</exception>
    Public Function GetUShort(ByVal index As Integer) As UShort
        Dim val As Object = GetItem(index)
        Dim result As UShort = JSON.toUShort(val)
        If Microsoft.VisualBasic.IsNothing(result) Then
            Throw JSON.typeMismatch(index, val, GetType(UShort).Name)
        End If
        Return result
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果该值存在并且是UShort或可以*强制为UShort）。否则返回<code>defaultVal</code>。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <param name="defaultVal"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OptUShort(ByVal index As Integer, Optional ByVal defaultVal As UShort = 0) As UShort
        Dim val As Object = Opt(index)
        Dim result As UShort = JSON.toUShort(val)
        Return IIf(Microsoft.VisualBasic.IsNothing(result), defaultVal, result)
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果它存在且是Integer或可以强制为Integer）。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' <exception cref="Exception">如果<code>index</code>处的值不存在或无法强制为Integer。</exception>
    Public Function GetInteger(ByVal index As Integer) As Integer
        Dim val As Object = GetItem(index)
        Dim result As Integer = JSON.toInteger(val)
        If Microsoft.VisualBasic.IsNothing(result) Then
            Throw JSON.typeMismatch(index, val, GetType(Integer).Name)
        End If
        Return result
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果该值存在并且是Integer或可以*强制为Integer）。否则返回<code>defaultVal</code>。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <param name="defaultVal"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OptInteger(ByVal index As Integer, Optional ByVal defaultVal As Integer = 0) As Integer
        Dim val As Object = Opt(index)
        Dim result As Integer = JSON.toInteger(val)
        Return IIf(Microsoft.VisualBasic.IsNothing(result), defaultVal, result)
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果它存在且是UInteger或可以强制为UInteger）。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' <exception cref="Exception">如果<code>index</code>处的值不存在或无法强制为UInteger。</exception>
    Public Function GetUInteger(ByVal index As Integer) As UInteger
        Dim val As Object = GetItem(index)
        Dim result As UInteger = JSON.toUInteger(val)
        If Microsoft.VisualBasic.IsNothing(result) Then
            Throw JSON.typeMismatch(index, val, GetType(UInteger).Name)
        End If
        Return result
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果该值存在并且是UInteger或可以*强制为UInteger）。否则返回<code>defaultVal</code>。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <param name="defaultVal"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OptUInteger(ByVal index As Integer, Optional ByVal defaultVal As UInteger = 0) As UInteger
        Dim val As Object = Opt(index)
        Dim result As UInteger = JSON.toUInteger(val)
        Return IIf(Microsoft.VisualBasic.IsNothing(result), defaultVal, result)
    End Function

    ''' <summary>
    ''' 返回<code>index</code>处的值（如果它存在且是Long或可以强制为Long）。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' <exception cref="Exception">如果<code>index</code>处的值不存在或无法强制为Long。</exception>
    Public Function GetLong(ByVal index As Integer) As Long
        Dim val As Object = GetItem(index)
        Dim result As Long = JSON.toLong(val)
        If Microsoft.VisualBasic.IsNothing(result) Then
            Throw JSON.typeMismatch(index, val, GetType(Long).Name)
        End If
        Return result
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果该值存在并且是Long或可以*强制为Long）。否则返回<code>defaultVal</code>。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <param name="defaultVal"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OptLong(ByVal index As Integer, Optional ByVal defaultVal As Long = 0) As Long
        Dim val As Object = Opt(index)
        Dim result As Long = JSON.toLong(val)
        Return IIf(Microsoft.VisualBasic.IsNothing(result), defaultVal, result)
    End Function

    ''' <summary>
    ''' 返回<code>index</code>处的值（如果它存在且是ULong或可以强制为ULong）。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' <exception cref="Exception">如果<code>index</code>处的值不存在或无法强制为ULong。</exception>
    Public Function GetULong(ByVal index As Integer) As ULong
        Dim val As Object = GetItem(index)
        Dim result As ULong = JSON.toULong(val)
        If Microsoft.VisualBasic.IsNothing(result) Then
            Throw JSON.typeMismatch(index, val, GetType(ULong).Name)
        End If
        Return result
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果该值存在并且是ULong或可以*强制为ULong）。否则返回<code>defaultVal</code>。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <param name="defaultVal"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OptULong(ByVal index As Integer, Optional ByVal defaultVal As ULong = 0) As ULong
        Dim val As Object = Opt(index)
        Dim result As ULong = JSON.toULong(val)
        Return IIf(Microsoft.VisualBasic.IsNothing(result), defaultVal, result)
    End Function

    ''' <summary>
    ''' 返回<code>index</code>处的值（如果它存在且是Single或可以强制为Single）。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' <exception cref="Exception">如果<code>index</code>处的值不存在或无法强制为Single。</exception>
    Public Function GetSingle(ByVal index As Integer) As Single
        Dim val As Object = GetItem(index)
        Dim result As Single = JSON.toSingle(val)
        If Microsoft.VisualBasic.IsNothing(result) Then
            Throw JSON.typeMismatch(index, val, GetType(Single).Name)
        End If
        Return result
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果该值存在并且是Single或可以*强制为Single）。否则返回<code>defaultVal</code>。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <param name="defaultVal"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OptSingle(ByVal index As Integer, Optional ByVal defaultVal As Single = 0.0) As Single
        Dim val As Object = Opt(index)
        Dim result As Single = JSON.toSingle(val)
        Return IIf(Microsoft.VisualBasic.IsNothing(result), defaultVal, result)
    End Function

    ''' <summary>
    ''' 返回<code>index</code>处的值（如果它存在且是Double或可以强制为Double）。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' <exception cref="Exception">如果<code>index</code>处的值不存在或无法强制为Double。</exception>
    Public Function GetDouble(ByVal index As Integer) As Double
        Dim val As Object = GetItem(index)
        Dim result As Double = JSON.toDouble(val)
        If Microsoft.VisualBasic.IsNothing(result) Then
            Throw JSON.typeMismatch(index, val, GetType(Double).Name)
        End If
        Return result
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果该值存在并且是Double或可以*强制为Double）。否则返回<code>defaultVal</code>。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <param name="defaultVal"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OptDouble(ByVal index As Integer, Optional ByVal defaultVal As Double = 0.0) As Double
        Dim val As Object = Opt(index)
        Dim result As Double = JSON.toDouble(val)
        Return IIf(Microsoft.VisualBasic.IsNothing(result), defaultVal, result)
    End Function

    ''' <summary>
    ''' 返回<code>index</code>处的值（如果它存在且是String或可以强制为String）。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' <exception cref="Exception">如果<code>index</code>处的值不存在或无法强制为String。</exception>
    Public Function GetString(ByVal index As Integer) As String
        Dim val As Object = GetItem(index)
        Dim result As String = JSON.ToString(val)
        If Microsoft.VisualBasic.IsNothing(result) Then
            Throw JSON.typeMismatch(index, val, GetType(String).Name)
        End If
        Return result
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果该值存在并且是String或可以*强制为String）。否则返回<code>defaultVal</code>。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <param name="defaultVal"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OptString(ByVal index As Integer, Optional ByVal defaultVal As String = Nothing) As String
        Dim val As Object = Opt(index)
        Dim result As String = JSON.ToString(val)
        Return IIf(Microsoft.VisualBasic.IsNothing(result), defaultVal, result)
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果该值存在并且是或可以强制为<code>JSONArray</code>）。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetJSONArray(ByVal index As Integer) As JSONArray
        Dim val As Object = GetItem(index)
        If val.GetType.Equals(GetType(JSONArray)) Then
            Return val
        ElseIf IsArray(val) Or val.GetType.Equals(GetType(ArrayList)) Or val.GetType.Equals(GetType(IList(Of Object))) Or val.GetType.Equals(GetType(List(Of Object))) Then
            Return New JSONArray(val)
        Else
            Throw JSON.typeMismatch(index, val, "JSONArray")
        End If
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果该值存在并且是或可以强制为<code>JSONArray</code>）。否则返回Nothing。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OptJSONArray(ByVal index As Integer) As JSONArray
        Dim val As Object = Opt(index)
        If val.GetType.Equals(GetType(JSONArray)) Then
            Return val
        ElseIf IsArray(val) Or val.GetType.Equals(GetType(ArrayList)) Or val.GetType.Equals(GetType(IList(Of Object))) Or val.GetType.Equals(GetType(List(Of Object))) Then
            Return New JSONArray(val)
        Else
            Return Nothing
        End If
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果该值存在并且是或者强制转换为<code>JSONObject</code>）。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetJSONObject(ByVal index As Integer) As JSONObject
        Dim val As Object = GetItem(index)
        If val.GetType.Equals(GetType(JSONObject)) Then
            Return val
        ElseIf val.GetType.Equals(GetType(Dictionary(Of String, Object))) Then
            Dim d As Dictionary(Of String, Object) = val
            Return New JSONObject(d)
        ElseIf val.GetType.Equals(GetType(SortedDictionary(Of String, Object))) Then
            Dim sd As SortedDictionary(Of String, Object) = val
            Dim d As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
            For Each kvp As KeyValuePair(Of String, Object) In sd   '遍历元素
                d.Add(kvp.Key, kvp.Value)
            Next
            Return New JSONObject(d)
        ElseIf val.GetType.Equals(GetType(String)) Then
            Return New JSONObject(CStr(val))
        Else
            Throw JSON.typeMismatch(index, val, "JSONObject")
        End If
    End Function
    ''' <summary>
    ''' 返回<code>index</code>处的值（如果该值存在并且是或可以强制为<code>JSONObject</code>）。否则返回Nothing。
    ''' </summary>
    ''' <param name="index"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OptJSONObject(ByVal index As Integer) As JSONObject
        Dim val As Object = GetItem(index)
        Try
            If val.GetType.Equals(GetType(JSONObject)) Then
                Return val
            ElseIf val.GetType.Equals(GetType(Dictionary(Of String, Object))) Then
                Dim d As Dictionary(Of String, Object) = val
                Return New JSONObject(d)
            ElseIf val.GetType.Equals(GetType(SortedDictionary(Of String, Object))) Then
                Dim sd As SortedDictionary(Of String, Object) = val
                Dim d As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
                For Each kvp As KeyValuePair(Of String, Object) In sd   '遍历元素
                    d.Add(kvp.Key, kvp.Value)
                Next
                Return New JSONObject(d)
            ElseIf val.GetType.Equals(GetType(String)) Then
                Return New JSONObject(CStr(val))
            Else
                Return Nothing
            End If
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    ''' <summary>
    ''' 返回一个新对象，其值是此数组中的值，其名称是<code>names</code>中的值。名称和值按索引从0到较短数组长度配对。不是字符串的名称将强制为字符串。如果任一数组为空，则此方法返回Nothing。
    ''' </summary>
    ''' <param name="names"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function toJSONObject(ByVal names As JSONArray) As JSONObject
        Dim result As JSONObject = New JSONObject()
        Dim length As Integer = Math.Min(names.Count, values.Count)
        If length = 0 Then
            Return Nothing
        End If
        For i = 0 To length - 1
            Dim name As String = JSON.ToString(names.Opt(i))
            result.put(name, Opt(i))
        Next
        Return result
    End Function
    ''' <summary>
    ''' 通过将此数组的值与<code>separator</code>交替返回新字符串。此数组的字符串值被引用，并对其特殊字符进行转义。例如，包含字符串'12" pizza', 'taco' 和 'soda' 的数组在'+'上连接返回：
    ''' "12\" pizza"+"taco"+"soda"
    ''' </summary>
    ''' <param name="separator"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Join(ByVal separator As String) As String
        Dim stringer As JSONStringer = New JSONStringer()
        stringer.open(JSONStringer.Scope.NULL, "")
        For i As Integer = 0 To values.Count - 1
            If i > 0 Then
                stringer.out.Append(separator)
            End If
            stringer.value(values.Item(i))
        Next
        stringer.close(JSONStringer.Scope.NULL, JSONStringer.Scope.NULL, "")
        Return stringer.out.ToString()
    End Function
    ''' <summary>
    ''' 将此数组编码为紧凑的JSON字符串，例如：[94043,90210]
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function toString() As String
        Try
            Dim stringer As JSONStringer = New JSONStringer()
            writeTo(stringer)
            Return stringer.toString()
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    ''' <summary>
    ''' 将此数组编码为用于调试的可读JSON字符串，例如：[94043, 90210]
    ''' </summary>
    ''' <param name="indentSpaces">嵌套的每个级别缩进的空格数。</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overloads Function toString(ByVal indentSpaces As Integer) As String
        Dim stringer As JSONStringer = New JSONStringer(indentSpaces)
        writeTo(stringer)
        Return stringer.toString()
    End Function
    Friend Sub writeTo(stringer As JSONStringer)
        stringer.startArray()
        For Each val As Object In values
            stringer.value(val)
        Next
        stringer.endArray()
    End Sub
    Public Overloads Function Equals(ByVal obj As Object) As Boolean
        If obj.GetType.Equals(GetType(JSONArray)) Then
            Return obj.values.Equals(values)
        End If

        Return False
    End Function
    Public Overloads Function GetHashCode() As Integer
        Return values.GetHashCode()
    End Function
    ''' <summary>
    ''' 类型是数字类型
    ''' </summary>
    ''' <param name="dataType">类型</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function IsNumeric(ByVal dataType As Type) As Boolean
        If Microsoft.VisualBasic.IsNothing(dataType) Then
            Return False
        Else
            Return dataType.Equals(GetType(Byte)) Or dataType.Equals(GetType(SByte)) Or dataType.Equals(GetType(Char)) Or _
                   dataType.Equals(GetType(Short)) Or dataType.Equals(GetType(UShort)) Or dataType.Equals(GetType(Integer)) Or _
                   dataType.Equals(GetType(UInteger)) Or dataType.Equals(GetType(Long)) Or dataType.Equals(GetType(ULong)) Or _
                   dataType.Equals(GetType(Single)) Or dataType.Equals(GetType(Double)) Or dataType.Equals(GetType(Int16)) Or _
                   dataType.Equals(GetType(Int32)) Or dataType.Equals(GetType(Int64)) Or dataType.Equals(GetType(UInt16)) Or _
                   dataType.Equals(GetType(UInt32)) Or dataType.Equals(GetType(UInt64)) Or dataType.Equals(GetType(UIntPtr))
        End If
    End Function

    ''' <summary>
    ''' 返回此数组中的值数。
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Count() As Integer ' Implements ICollection(Of Object).Count
        Get
            Return values.Count
        End Get
    End Property
    Public Sub Clear()
        values.Clear()
    End Sub
    Public Sub CopyTo(array As Object(), arrayIndex As Integer)
        If array Is Nothing Then
            Throw New ArgumentNullException("The array cannot be null.")
        ElseIf arrayIndex < 0 Then
            Throw New ArgumentOutOfRangeException("The starting array index cannot be negative.")
        ElseIf Count > array.Length - arrayIndex + 1 Then
            Throw New ArgumentException("The destination array has fewer elements than the collection.")
        End If
        ' For i As Integer = 0 To listArray.Length - 1
        'array(i + arrayIndex) = listArray(i)
        ' Next
        values.CopyTo(array, arrayIndex)
    End Sub

End Class
